$(function () {

  class Cart {
    constructor() {
      this.ID = cookie('ID')
      this.TOKEN = cookie('Token')
      if(this.ID.length>0 && this.TOKEN.length>0){
      }else{
        alert('请先登录')
        setTimeout(()=>{
          location.href =  '../../dist/login.html'
        },1000)
      }
      this.goods_msg = JSON.parse(localStorage.getItem('goods_msg'));
      this.payment = $('.payment .payamount')
      this.cart_item = $('.cart_item')
      this.buy = $('.payment .buy')
      this._count = 0

      this.render();

      this.incre_btn = $('.order-quantity .incre button')
      this.decre_btn = $('.order-quantity .decre button')

      //Number($(e.target).parent().prev().val())++
      this.bind();

      this.total();
    }
    render() {
      this.cart_item.html(this.goods_msg.map(item => `
        <div class="info-detail">
          <img class='info_img' src='${item.goods_pic}'>
          <div class='info-msg cf'>
            <div class='info_title'>${item.goods_title}</div>
            <div class='info_icon_list'>
              <div>
                <img src='https://img.alicdn.com/imgextra/i4/O1CN01EsdJve1xNRnNjF6PP_!!6000000006431-2-tps-48-16.png'>
                <img src='https://img.alicdn.com/tps/i3/T1Vyl6FCBlXXaSQP_X-16-16.png'>
                <img src='https://img.alicdn.com/tps/i4/T1BCidFrNlXXaSQP_X-16-16.png'>
                <img src='https://assets.alicdn.com/sys/common/icon/trade/xcard.png'>
                <img src='https://img.alicdn.com/imgextra/i4/O1CN01OP1qo91vEcwA4pw84_!!6000000006141-2-tps-84-28.png'>
              </div>
              <p class='ship-time'>
                <span>发货时间</span>店铺预售,付款后5天内
              </p>
            </div>
          </div>
          
        </div>
        <div class="info-price">${item.goods_single_price}</div>
        <div class="order-quantity">
          <span class="decre">
            <button>-</button>
          </span>
          <input type="text" value='${item.goods_count}'>
          <span class="incre">
            <button>+</button>
          </span>
        </div>
        <div class="discount">
          <span>省60.20:划算价</span>
          <i></i>
        </div>
      `).join(''))
    }
    total() {
      let mom = JSON.parse(localStorage.getItem('goods_msg'))
      let sum = 0;
      for(let i=0;i<mom.length;i++){
        sum += mom[i].goods_count * mom[i].goods_single_price
      }
      this.payment.html(sum)
    }
    bind() {
      this.incre_btn.on('click', (e) => {
        this._count = $(e.target).parent().prev().val()
        this._count = Number(this._count)
        this._count++;

        let m_src = $(e.target).parent().parent().prev().prev().find('.info_img').attr('src')
        for (let i = 0; i < this.goods_msg.length; i++) {
          if (this.goods_msg[i].goods_pic === m_src) {

            this.goods_msg[i].goods_count++
          }
        }
        localStorage.setItem('goods_msg', JSON.stringify(this.goods_msg))
        console.log(this.goods_msg);
        $(e.target).parent().prev().val(this._count)
        this.total()
      })

      this.decre_btn.on('click', (e) => {
        this._count = $(e.target).parent().next().val()
        this._count = Number(this._count)

        if (this._count > 0) {
          this._count--;
          let m_src = $(e.target).parent().parent().prev().prev().find('.info_img').attr('src')
          for (let i = 0; i < this.goods_msg.length; i++) {
            if (this.goods_msg[i].goods_pic === m_src) {

              if(this.goods_msg[i].goods_count>1){

                this.goods_msg[i].goods_count--
              }else if(this.goods_msg[i].goods_count <= 1){
                this.goods_msg.splice(i,1)
                location.reload()
              }
            }
          }
          localStorage.setItem('goods_msg', JSON.stringify(this.goods_msg))
          // console.log(this.goods_msg);
          $(e.target).parent().next().val(this._count)
          this.total()
        }
        else {
        }

      })

      this.buy.on('click',()=>{
        if(this.goods_msg.length>0){

          alert('恭喜你，购买成功。你是最胖的!!!')
          let arrn = []
          localStorage.setItem('goods_msg',JSON.stringify(arrn))
          location.reload()
        }else{
          alert('您的购物车还没有商品，先去挑选哦!')
        }

      })
    }
  }
  new Cart();

  class Index {
    constructor() {
      this.defaultmsg = $('.nav-head .defaultmsg')
      this.showmsg = $('.showmsg')
      this.nickname = $('.showmsg .nickname')
      this.quitbtn = $('.showmsg .quit')
      this.id = cookie('ID');
      this.token = cookie('Token');
      if (this.id.length > 0 && this.token.length > 0) {
        this.defaultmsg.css({
          display: 'none'
        })
        this.showmsg.css({
          display: 'block'
        })
        this.getUserInfo()
      }

      this.bindEvent()
    }

    getUserInfo() {
      let options = {
        url: 'http://localhost:8888/users/info/' + this.id,
        headers: {
          "authorization": this.token
        }

      }

      $.ajax(options).then(res => {
        this.render(res)
      })
    }

    render(res) {
      console.log(res);
      this.nickname.text(res.info.nickname);
    }
    quitUser() {
      let options = {
        url: 'http://localhost:8888/users/logout/' + this.id,
      }

      $.ajax(options).then(res => {
        console.log('退出账户成功');
        cookie('ID', null)
        cookie('Token', null)
        setTimeout(() => {
          location.reload()
        }, 500)
      })
    }
    bindEvent() {
      this.quitbtn.on('click', () => {
        this.quitUser()
      })
    }
  }
  new Index();
})

$(function () {


})